﻿using Common.Buffer;
using Common.Mysql;
using Common.NetWork;
using Common.Server;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    /// 帐号在线状态
    /// </summary>
    public enum ACCOUNTSSATEENUM:int
    {
        离线 = -1,
        登陆 = 0,
        在线 = 1,        
        商城 = 2, 
        拍卖 = 3, 
        频道 = 4, 
    };

    /// <summary>
    /// 冒险岛客户端
    /// </summary>
    public class MapleClient
    {
        /// <summary>
        /// 消息发送
        /// </summary>
        private MapleSession m_Session;

        /// <summary>
        /// 客户端发包加密
        /// </summary>
        private MapleAESOFB m_SendAES;

        /// <summary>
        /// 服务端接收解密包
        /// </summary>
        private MapleAESOFB m_RecAES;

        /// <summary>
        /// 帐号ID
        /// </summary>
        private long m_Id;

        /// <summary>
        /// 性别
        /// </summary>
        private int m_gender;

        /// <summary>
        /// 用户名
        /// </summary>
        private string m_UseName;

        /// <summary>
        /// 数据库连接池引用
        /// </summary>
        private MapleMySqlManager m_SqlConn;

        /// <summary>
        /// 帐号状态
        /// </summary>
        private ACCOUNTSSATEENUM m_State = ACCOUNTSSATEENUM.离线;


        public MapleClient(MapleSession session, MapleAESOFB Rec, MapleAESOFB Send)
        {
            m_SendAES = Send;
            m_RecAES = Rec;
            m_Session = session;
            m_SqlConn = Common.Mysql.MysqlPool.MysqlConnectPool.GetInstance().GetConnection(this);
        }

        public int GameId = 0;

        /// <summary>
        /// 发送封包
        /// </summary>
        /// <param name="data"></param>
        public void Send(MaplePacket data)
        {
            /*
            byte[] ret = new byte[data.Length + 4];

            byte[] header = m_SendAES.getPacketHeader(data.Length);

            m_SendAES.crypt(data);

            System.Buffer.BlockCopy(header, 0, ret, 0, 4);

            System.Buffer.BlockCopy(data, 0, ret, 4, data.Length);
            */
           

            m_Session.Send(data);
        }

        public void GameSend(byte[] a)
        {
            int ad = 0;
        }

        public MapleSession GetSession()
        {
            return m_Session;
        }


        /// <summary>
        /// 登录检测
        /// </summary>
        /// <param name="Use"></param>
        /// <param name="Pass"></param>
        /// <returns></returns>
        public int Login(string Use, string Pass)
        {
            ///登录状态 -1:帐号不存在 0登录成功! 1密码错误
            int state = -1;

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            //查询用户名
            string com = "select name,id from accounts where name='" + Use + "'";
            MySqlDataReader reader = m_SqlConn.GetMysqlRead(com);//Mysql.MysqlPool.MysqlConnectPool.GetInstance().GetConnection( //MapleMySqlManager.m_Ins.GetMysqlRead(com);

            if (reader !=null && reader.Read())
            {
                m_UseName = (string)reader["name"];
                m_Id = long.Parse(reader["id"].ToString());

                com = "select password,loggedin from accounts where name='" + m_UseName + "' or password='" + Pass + "'";
                reader = m_SqlConn.GetMysqlRead(com);//MapleMySqlManager.m_Ins.GetMysqlRead(com);

                if (reader.Read())
                {
                    //如果该帐号在线..

                    Console.WriteLine(reader["loggedin"]);
                    m_State = (ACCOUNTSSATEENUM)int.Parse(reader["loggedin"].ToString());
                    if (m_State >= ACCOUNTSSATEENUM.离线)
                    {
                        //登录成功!
                        state = 0;
                    }
                    else
                    {
                        //该帐号已登录
                        state = 2;
                    }                    
                }
                else
                {
                    //密码错误!!
                    state = 1;
                }
            }
            stopwatch.Stop();
            TimeSpan timespan = stopwatch.Elapsed;
            Console.WriteLine("事件:" + timespan.TotalSeconds);




            return state;
        }


        /// <summary>
        /// 检测帐号
        /// </summary>
        /// <param name="Nmae"></param>
        /// <returns></returns>
        public bool isCheckAccount(string Nmae)
        {
            bool isUse = true;

            string com = "select name from accounts where name='" + Nmae + "'";
            MySqlDataReader reader = m_SqlConn.GetMysqlRead(com);//MapleMySqlManager.m_Ins.GetMysqlRead(com);
            if (reader != null && reader.Read())
            {
                isUse = false;
            }

            return isUse;
        }


        /// <summary>
        /// 注册帐号
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public bool isRegisterAccount(string Name, string pass, string email, short Sex)
        {
            bool isUse = false;
            try
            {
                string com = "insert into accounts(name,password,email,gender) values('" + Name + "','" + pass + "','" + email + "'," + Sex + ");";
                //MapleMySqlManager.m_Ins.GetMysqlCom(com);
                m_SqlConn.GetMysqlCom(com);
                //Common.Mysql.MysqlPool.MysqlConnectPool.GetInstance().GetConnection(1111).GetMysqlCom(com);
                isUse = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("SQL错误:" + e);
            }

            return isUse;
        }

        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <returns></returns>
        public string GetUseName()
        {
            return m_UseName;
        }

        /// <summary>
        /// 获取性别
        /// </summary>
        /// <returns></returns>
        public byte GetGender()
        {
            return (byte)m_gender;
        }

        /// <summary>
        /// 获取帐号ID
        /// </summary>
        /// <returns></returns>
        public long GetID()
        {
            return m_Id;
        }


        public int GameID()
        {
            return GameId;
        }

    }
}
